
import inspect

D_ERROR = (0, 'E')
D_WARN = (1, 'W')
D_INFO = (2, 'I')
D_DEBUG = (3, 'D')

debug_level = D_INFO

def is_higher(l1, l2):
  return l1[0] < l2[0]

def is_lower(l1, l2):
  return l1[0] > l2[0]

def equal(l1, l2):
  return l1[0] == l2[0]

def debug(s, level=D_DEBUG):
  if not is_lower(level, debug_level):
    frame = get_frame_of_debug_caller()
    fn, ln_num, func = frame[1], frame[2], frame[3]
    fn_el = fn.split('/')
    
    print '[%s][%s:%d - %s] %s' % (level[1], fn_el[len(fn_el) - 1], ln_num, func, s)

def info(s):
  debug(s, level=D_INFO)

def warn(s):
  debug(s, level=D_WARN)

def error(s):
  debug(s, level=D_ERROR)

def get_frame_of_debug_caller():
  f = inspect.stack()
  for ff in f:
    #print ff[0]
    if ff[1].find('debug') == -1:
      return ff
  return None


if __name__ == "__main__":
  get_frame_of_debug_caller()
